<?php
	/*==================================================================*/
	/*		文件名:Column.class.php                              */
	/*		概要: 文章        	       	    */
	/*==================================================================*/
	class ArticleModel extends Model {
		function __construct(){
			parent:: __construct();
			$this->tabName = TAB_PREFIX."article";
			$this->gtabName=TAB_PREFIX."picture";
			$this->fieldList=Array("id","title","summary","postTime","author","comeFrom","views", "content","keyword","pid","audit","picId","recommend");
		}
		function validateForm(){
			$result=true;
			if(!Validate::required($_POST['title'])){
				$this->messList[] = "文章标题不能为空.";
				$result=false;
			}
			if(!Validate::checkLength($_POST['title'], 150)) {
				$this->messList[] = "文章标题不能超过50个汉字.";
				$result=false;
			}
			if(!Validate::required($_POST['postTime'])){
				$this->messList[] = "发布时间不能为空.";
				$result=false;
			}
			if(!Validate::match($_POST['postTime'], "/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/")) {
				$this->messList[] = "发布时间格式不正确.";		
				$result=false;
			}
			if(!Validate::checkLength($_POST['summary'], 300)) {
				$this->messList[] = "文章摘要不能超过100个汉字.";
				$result=false;
			}
			if(!Validate::required($_POST['author'])) {
				$this->messList[] = "文章作者不能为空.";
				$result=false;
			}
			if(!Validate::checkLength($_POST['author'], 30)) {
				$this->messList[] = "文章作者不能超过10个汉字.";
				$result=false;
			}
			if(!Validate::required($_POST['keyword'])) {
				$this->messList[] = "关键字不能为空.";
				$result=false;
			}
			if(!Validate::checkLength($_POST['keyword'], 290)){
				$this->messList[] = "关键字不能超过30个汉字.";
				$result=false;
			}
			if(!Validate::required($_POST['content'])) {
				$this->messList[] = "文章内容不能为空.";
				$result=false;
			}
			return  $result;
		}	
		function find($id) {
			$art=parent::find($id);
			if($art["picId"]!=0){
				$fieldList=array();
				foreach($this->fieldList as $key=>$value){
					$fieldList[$key]="tab1.".$value;
				}	
				$filed=join(",", $fieldList);
				$sql="select {$filed},tab2.picName from {$this->tabName} as tab1,{$this->gtabName} as tab2 where tab1.picId=tab2.id and tab1.id={$id}";
			}else{
				$filed=join(",", $this->fieldList);
				$sql="select {$filed} from {$this->tabName}  where id={$id}";
			}
			$result=$this->mysqli->query($sql);
			if($result && $result->num_rows >0){
				return $result->fetch_assoc();
			}else{
				return null;
			}
	
		}

		function audit($id, $status=1){
			$sql = "UPDATE {$this->tabName} SET audit={$status} WHERE id ";
			if(is_array($id)) {
				$sql .= "IN (" . join(",", $id) . ")";

			}else{
				$sql .= "= $id";
			}

			if($this->mysqli->query($sql)){

				$this->messList[] = $status?"审核成功！":"锁定成功！";
				return true;
			}else{
				$this->messList[] = $status?"审核失败！":"锁定失败！";
				return false;
			}
		}

		function getPlayPic($num) {
			$sql="SELECT t1.id, t1.title, t2.picName,t2.hasMark from {$this->tabName} as t1, {$this->gtabName} as t2 where t1.picId=t2.id and t1.picId <> 0 and t1.recommend=1 and audit=1 order by id desc limit 0, {$num}";

			//pr($sql);
			$result=$this->mysqli->query($sql);

			if($result && $result->num_rows >0){
				$data=array();

				while($row=$result->fetch_assoc()){
					if($row["hasMark"]==1){
						$row["picName"]="wa_".$row["picName"];
					}
					$data[]=$row;
				}

				return $data;
			}else{
				return null;
			}
		}

		function setViews($id){
			$this->mysqli->query("UPDATE {$this->tabName} SET views=views+1 WHERE id={$id}");
		}

		function getNextArticle($pid, $id){
			$sql="SELECT  id, title,author,postTime FROM {$this->tabName} WHERE  id > {$id} and audit=1 and pid={$pid} ORDER BY id ASC LIMIT 0, 1";
			$result=$this->mysqli->query($sql);

			if($result && $result->num_rows ==1){
				return $result->fetch_assoc();
			}else{
				return false;
			}
		}


		function getPrevArticle($pid, $id){
			$sql="SELECT  id, title,author,postTime FROM {$this->tabName} WHERE  id < {$id} and audit=1 and pid={$pid} ORDER BY id DESC LIMIT 0, 1";
			$result=$this->mysqli->query($sql);

			if($result && $result->num_rows ==1){
				return $result->fetch_assoc();
			}else{
				return false;
			}
		}
		function getSearchResult($type, $condition, $offset, $num){
			$sql = "SELECT id, title,author,postTime,views FROM {$this->tabName} WHERE audit=1 ";	
			$tmp = "";
			$condition=trim($condition);
			switch($type){
				case "title":               //按标题搜索
					$tmp .= "AND title LIKE '%{$condition}%' ";
					break;
				case "content":               //按内容搜索
					$tmp .= "AND content LIKE '%{$condition}%' ";
					break;
				case "keyword":               //按关键字搜索
					$tmp .= "AND keyword LIKE '%{$condition}%' ";
					break;
			}

			$tmp .= "ORDER BY postTime DESC LIMIT {$offset}, {$num}";
			$sql .= $tmp;
		
			$result=$this->mysqli->query($sql);
			while($row=$result->fetch_assoc()){
				$data[]=$row;
			}
			return $data;
		}


		function getSearchTotal($type, $condition){
			$sql = "SELECT id, title,author,postTime,views FROM {$this->tabName} WHERE audit=1 ";	
			$tmp = "";
			$condition=trim($condition);
			switch($type){
				case "title":               //按标题搜索
					$tmp .= "AND title LIKE '%{$condition}%' ";
					break;
				case "content":               //按内容搜索
					$tmp .= "AND content LIKE '%{$condition}%' ";
					break;
				case "keyword":               //按关键字搜索
					$tmp .= "AND keyword LIKE '%{$condition}%' ";
					break;
			}

			$tmp .= "ORDER BY postTime DESC";
			$sql .= $tmp;

			$result=$this->mysqli->query($sql);
			return $result->num_rows;	
		}


	}
